﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_BookFoodUpdForm
 * 类型：       控制层
 * 功能说明：   监控
 * 作者：       郭晓庆
 * 创建日期：   2008/02/01
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Utils;
using Shpm.App.Input.Bean;
using Shpm.System;
using Shpm.App.Common.Dao;
using Shpm.App.Common.Bean;

public partial class App_Input_BookFoodUpdForm : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        //保持中文星期名称
        this.Label1.Text = this.hidWeek.Value;

       


        if(!IsPostBack )
        {
            BookFoodDao dao = new BookFoodDao();

            //时间下拉初始化
            addTime(this.dropHour, 23);
            addTime(this.dropMin, 59);
            dropHour.SelectedIndex = DateTime.Now.Hour;
            dropMin.SelectedIndex = DateTime.Now.Minute;
            //
            ViewState["MEMBER_NO"] = Context.Items["MEMBER_NO"];
            ViewState["DINNER_DATE"] = Context.Items["DINNER_DATE"];
            ViewState["SEND_DATE"]= Context.Items["SEND_DATE"];
            ViewState["SEND_DATE_TO"]=Context.Items["SEND_DATE_TO"];
            ViewState["HOUR"]= Context.Items["HOUR"];
            ViewState["HOUR_TO"]= Context.Items["HOUR_TO"];
            ViewState["MIN"]= Context.Items["MIN"];
            ViewState["MIN_TO"]=Context.Items["MIN_TO"];

            ViewState["TXT_MEMBER_NO"] = Context.Items["TXT_MEMBER_NO"];
            ViewState["TXT_DINNER_DATE"] = Context.Items["TXT_DINNER_DATE"];
            ViewState["TXT_SEND_DATE"] = Context.Items["TXT_SEND_DATE"];
            ViewState["TXT_SEND_DATE_TO"] = Context.Items["TXT_SEND_DATE_TO"];
            ViewState["DROP_HOUR"] = Context.Items["DROP_HOUR"];
            ViewState["DROP_HOUR_TO"] = Context.Items["DROP_HOUR_TO"];
            ViewState["DROP_MIN"] = Context.Items["DROP_MIN"];
            ViewState["DROP_MIN_TO"] = Context.Items["DROP_MIN_TO"];

            //drop绑定
            CommonDictionaryDao commonDictionaryDao = new CommonDictionaryDao();
            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            paramBean.Catalog = "M_DISH.DISH_TIME";
            IList dataList = commonDictionaryDao.FindByCatalog(paramBean);
            dataList.Insert(0, new CommonDictionaryBean());
            this.dropDinnerType.DataSource = dataList;
            this.dropDinnerType.DataBind();
            dataList.RemoveAt(0);
            this.dropDinnerType2.DataSource = dataList;
            this.dropDinnerType2.DataBind();

            paramBean.Catalog = "M_DISH.DISH_TYPE";
            dataList = commonDictionaryDao.FindByCatalog(paramBean);
            dataList.Insert(0, new CommonDictionaryBean());
            this.dropType.DataSource = dataList;
            this.dropType.DataBind();


            paramBean.Catalog = "M_DISH.DISH_CLASS";
            dataList = commonDictionaryDao.FindByCatalog(paramBean);
            dataList.Insert(0, new CommonDictionaryBean());
            this.dropClass.DataSource = dataList;
            this.dropClass.DataBind();

            //编辑
            if (Context.Items.Contains("DINNER_ID"))
            {
                //送餐信息
                this.labDinnerId.Text = Convert.ToString(Context.Items["DINNER_ID"]);
                MemberDinnerBean memberDinnerBean = new MemberDinnerBean();
                memberDinnerBean.DinnerId = Convert.ToInt32(Context.Items["DINNER_ID"]);
                memberDinnerBean = dao.GetMemberDinner(memberDinnerBean);
                this.txtMemberNo.Text = memberDinnerBean.MemberNo;
                this.txtSendAddress.Text = memberDinnerBean.DinnerDelivery;
                this.txtSendDate.Text =  DateUtils.convertToString(memberDinnerBean.DinnerTime);
                this.dropHour.SelectedValue = memberDinnerBean.DinnerTime.Hour.ToString();
                this.dropMin.SelectedValue = memberDinnerBean.DinnerTime.Minute.ToString();
                this.dropDinnerType2.SelectedValue = memberDinnerBean.DinnerType;
                this.txtMemo.Text = memberDinnerBean.DinnerMemo;


                CommonSearchDao commonSearchDao = new CommonSearchDao();
                ComMemberBean beanMem = new ComMemberBean();
                beanMem.MemberNo = memberDinnerBean.MemberNo;
                beanMem = commonSearchDao.FindMember(beanMem);
                this.txtMemberName.Text = beanMem.NameC;
              

                //菜单信息
                MemberDinnerDetailBean memberDinnerDetailBean = new MemberDinnerDetailBean();
                memberDinnerDetailBean.DinnerId = Convert.ToInt32(Context.Items["DINNER_ID"]);
                IList memberDinnerDetailLst = dao.FindMemberDinnerDetail(memberDinnerDetailBean);
                this.GridView2.DataSource = memberDinnerDetailLst;
                this.GridView2.DataBind();
                ViewState["DISH_LIST"] = memberDinnerDetailLst;
            }
            //新建
            else 
            {
                this.txtSendDate.Text = DateUtils.convertToString(DateTime.Now);
                this.GridView2.DataSource = null;
                this.GridView2.DataBind();
            }

            //
            this.txtDate.Text = DateUtils.convertToString(DateTime.Now);
            string weekstr = DateTime.Now.DayOfWeek.ToString();
            int week = 0;
            switch (weekstr)
            {
                case "Monday": weekstr = "星期一"; week = 1; break;
                case "Tuesday": weekstr = "星期二"; week = 2; break;
                case "Wednesday": weekstr = "星期三"; week = 3; break;
                case "Thursday": weekstr = "星期四"; week = 4; break;
                case "Friday": weekstr = "星期五"; week = 5; break;
                case "Saturday": weekstr = "星期六"; week = 6; break;
                case "Sunday": weekstr = "星期日"; week = 0; break;
            }

            this.LabDate.Text = DateUtils.convertToString(DateTime.Now) + "(" + weekstr + ")";
            this.hidWeek.Value = weekstr;
            this.Label1.Text = weekstr;
            this.LabTime.Text = "全部";

           
           

            //菜单列表
           
            DishBean bean = new DishBean();
            bean.MenuDate = DateTime.Now;
            IList list = dao.FindDish(bean);
            if (list.Count <= 0)
            {
                bean.MenuDate  = null ;
                bean.MenuWeek = week;
                list = dao.FindDish(bean);
            }
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();


            this.txtMemberNo.Attributes.Add("onblur", "showMemberInfo(this.id,'" + this.txtMemberName.ClientID + "','" + this.txtSendAddress.ClientID + "');");
        }
        this.Literal1.Text = "";
    }

    /*时间下拉空间***/
    private void addTime(DropDownList obj,int n) 
    {
        string param = "";
        ListItem item = null;
        for (int i = 0; i <= n; i++)
        {
            param = i < 10 ?"0" + i:Convert.ToString(i);
            item = new ListItem();

            item.Value = param;
            item.Text  = param;
            obj.Items.Add(item);   
        }   
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.ComValidator1.DoValidator() == false)
        {

            ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

            return;
        }

        BookFoodDao dao = new BookFoodDao();
        DishBean bean = new DishBean();
        bean.MenuDate = DateUtils.convertToDate(this.txtDate.Text);
        bean.DishType = string.IsNullOrEmpty(this.dropType.SelectedValue) ? null : this.dropType.SelectedValue;
        bean.DishClass = string.IsNullOrEmpty(this.dropClass.SelectedValue) ? null : this.dropClass.SelectedValue;
        this.LabDate.Text = this.txtDate.Text+"("+this.hidWeek.Value+")";

        if (!string.IsNullOrEmpty(this.dropDinnerType.SelectedValue))
        {
            bean.MenuTime = this.dropDinnerType.SelectedValue;
            this.LabTime.Text = this.dropDinnerType.SelectedItem.Text;
        }
        else 
        {
           
            this.LabTime.Text = "全部";
        }
        ViewState["MEMU_DATE"] = bean.MenuDate;
        ViewState["TYPE"] = bean.DishType;
        ViewState["CLASS"] = bean.DishClass;
        ViewState["TIME"] = bean.MenuTime;
        IList list = dao.FindDish(bean);
        if(list.Count<=0)
        {
            bean.MenuDate  = null;

            string weekstr = Convert.ToDateTime(this.txtDate.Text).DayOfWeek.ToString();
            int week = 0;
            switch (weekstr)
            {
                case "Monday": week = 1; break;
                case "Tuesday": week = 2; break;
                case "Wednesday": week =3; break;
                case "Thursday": week = 4; break;
                case "Friday": week = 5; break;
                case "Saturday": week = 6; break;
                case "Sunday": week = 0; break;
            }
            bean.MenuWeek = week;
            list = dao.FindDish(bean);
        }
        this.GridView1.PageIndex = 0;
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
       
    }

    protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        //取得物资申领单内容
        IList list = null;
        if (ViewState["DISH_LIST"] == null)
        {
            list = new ArrayList();
        }
        else
        {
            list = (IList)ViewState["DISH_LIST"];
        }


        int dishId = Convert.ToInt32((sender as GridView).DataKeys[e.NewEditIndex].Values[0]);
 

        //取得前一画面的申领单数据

        MemberDinnerDetailBean paramBean = null;

        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            paramBean = (MemberDinnerDetailBean)list[i];
            paramBean.DishQuantity = Convert.ToInt32((this.GridView2.Rows[i].FindControl("quantity") as TextBox).Text);
        }

        //查找单个菜肴信息
        BookFoodDao dao = new BookFoodDao();
        MemberDinnerDetailBean bean = new MemberDinnerDetailBean();
        bean.DishId = dishId;
        bean = dao.GetMemberDinnerDetail(bean);

        //判断是否加入新的物资

        bool flg = true; //true:新物资；false:旧物资
        for (int i = 0; i < list.Count; i++)
        {
            paramBean = (MemberDinnerDetailBean)list[i];
            if (bean.DishId  == paramBean.DishId)
            {
                paramBean.DishQuantity = paramBean.DishQuantity == 99999?paramBean.DishQuantity:paramBean.DishQuantity + 1;
                flg = false;
                break;
            }
        }

        if (flg)
        {
            bean.DishQuantity = 1;
            list.Add(bean);
        }




        ViewState["DISH_LIST"] = list;
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            MemberDinnerDetailBean bean = (MemberDinnerDetailBean)e.Row.DataItem;
            TextBox quantity = ((TextBox)e.Row.FindControl("quantity"));
            quantity.Text = Convert.ToString(bean.DishQuantity);
            quantity.Attributes.Add("onblur", "checkQuantity('" + quantity.ClientID + "')");
            quantity.Attributes.Add("onfocus", "tempDate('" + quantity.ClientID + "')");
            e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);
            

        }
    }
    protected void GridView2_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        IList list = (ArrayList)ViewState["DISH_LIST"];
        int row = e.RowIndex;
        list.RemoveAt(row);
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);


        }
    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {
        //CHECK
        BookFoodDao dao = new BookFoodDao();

        //清除饮食建议提示和执行脚本
       // this.hidMessage.Value = "";
       
        if("FALSE".Equals(this.hidOrderFlg.Value))
        {
            
            if (this.ComValidator2.DoValidator() == false)
            {

                ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator2.ErrorMessage;

                return;
            }
             
            if (checkMemberNo() == false)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                              HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", this.txtMemberNo.Text);
                return;
            }

            IList list =  (IList)ViewState["DISH_LIST"];
            //取得前一画面的申领单数据

            MemberDinnerDetailBean paramBean = null;
            string quantity = null;
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                paramBean = (MemberDinnerDetailBean)list[i];
                quantity = (this.GridView2.Rows[i].FindControl("quantity") as TextBox).Text;
                
                try
                {
                    // 数値解析
                    long lvalue1 = long.Parse(quantity);
                }
                catch
                {
                    ((Design_MasterPage)this.Master).InfoMessage =
                            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0036").ToString().Replace("{0}", "数量");
            
                    return ;
                }
                paramBean.DishQuantity = Convert.ToInt32(quantity);
            }

            if (list == null || list.Count<=0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                     HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0094").ToString().Replace("{0}", "菜单内容"); ;
                return;
            }
            //饮食建议check

           
            IList foodList = new ArrayList();
            for (int i = 0; i < list.Count; i++)
            {
                paramBean = (MemberDinnerDetailBean)list[i];
                foodList.Add(paramBean.DishId);
            }
            DishBean bean = new DishBean();
            bean.MemberNo = this.txtMemberNo.Text;
            bean.FoodList = foodList;
            IList foodInfoList = dao.FindFoodInfo(bean);
            //如果有不宜食用的菜，弹出提示信息
            if (foodInfoList.Count>0)
            {
                string info = "";
                for (int i = 0; i < foodInfoList.Count; i++)
                {
                    bean = (DishBean)foodInfoList[i];
                    info = info + bean.DishName + "：" + bean.FoodAdvise + "\\n";
                }
                
                if (!string.IsNullOrEmpty(info))
                {
                    info = info + "是否继续订餐？";
                    this.Literal1.Text = "<script language=JavaScript for=window event=onload>  if(confirm('" + info + "')){"
                    + "window.document.getElementById('" + this.hidOrderFlg.ClientID + "').value='TRUE';"
                    + "window.document.getElementById('" + this.btnOrder.ClientID + "').click(); }</script>";
                }
               
                return;
            }
            this.hidOrderFlg.Value = "TRUE";
        }

        //生成餐饮订单
        

        if ("TRUE".Equals(this.hidOrderFlg.Value))
        {
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;

            MemberDinnerBean memberDinnerBean = new MemberDinnerBean();
            memberDinnerBean.MemberNo = this.txtMemberNo.Text;
            memberDinnerBean.DinnerDelivery = this.txtSendAddress.Text;
            memberDinnerBean.DinnerTime = Convert.ToDateTime(this.txtSendDate.Text + "  " + this.dropHour.SelectedValue + ":" + this.dropMin.SelectedValue);
            memberDinnerBean.DinnerMemo = this.txtMemo.Text;
            memberDinnerBean.DinnerType = this.dropDinnerType2.SelectedValue;
            memberDinnerBean.UserId = loginId;
            //编辑
            if (!string.IsNullOrEmpty(this.labDinnerId.Text))
            {
                memberDinnerBean.DinnerId = Convert.ToInt32(this.labDinnerId.Text);
                dao.Upd((IList)ViewState["DISH_LIST"], memberDinnerBean);
                ((Design_MasterPage)this.Master).InfoMessage =
                               HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
            }
            //新建
            else 
            {
               this.labDinnerId.Text = Convert.ToString(dao.Insert((IList)ViewState["DISH_LIST"], memberDinnerBean));
                ((Design_MasterPage)this.Master).InfoMessage =
                               HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
            }
           
           
           
            this.hidOrderFlg.Value = "FALSE";
        }
    }

    private bool checkMemberNo()
    {
        MemberSrhDao dao = new MemberSrhDao();
        MemberSrhBean bean = new MemberSrhBean();
        bean.MemberNo = this.txtMemberNo.Text;
        if (dao.GetMember(bean) == null)
            return false;
        return true;
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Context.Items["MEMBER_NO"] = ViewState["MEMBER_NO"];
        Context.Items["DINNER_DATE"] = ViewState["DINNER_DATE"];   
        Context.Items["SEND_DATE"] = ViewState["SEND_DATE"];
        Context.Items["SEND_DATE_TO"] = ViewState["SEND_DATE_TO"];
        Context.Items["HOUR"] = ViewState["HOUR"];
        Context.Items["HOUR_TO"] = ViewState["HOUR_TO"];
        Context.Items["MIN"] = ViewState["MIN"];
        Context.Items["MIN_TO"] = ViewState["MIN_TO"];

        Context.Items["TXT_MEMBER_NO"] = ViewState["TXT_MEMBER_NO"];
        Context.Items["TXT_DINNER_DATE"] = ViewState["TXT_DINNER_DATE"];
        Context.Items["TXT_SEND_DATE"] = ViewState["TXT_SEND_DATE"];
        Context.Items["TXT_SEND_DATE_TO"]=ViewState["TXT_SEND_DATE_TO"];
        Context.Items["DROP_HOUR"] = ViewState["DROP_HOUR"];
        Context.Items["DROP_HOUR_TO"] = ViewState["DROP_HOUR_TO"];
        Context.Items["DROP_MIN"] = ViewState["DROP_MIN"];
        Context.Items["DROP_MIN_TO"] = ViewState["DROP_MIN_TO"];

        string url = "~/App/Input/FoodOrdersSrhForm.aspx";
        ControllerUtils.Transfer(Context, url, true);   
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号


        //GridView1.PageIndex = e.NewPageIndex;
        //BookFoodDao dao = new BookFoodDao();
        //DishBean bean = new DishBean();
        //bean.MenuDate = Convert.ToDateTime(ViewState["MEMU_DATE"]);
        //bean.DishTime = string.IsNullOrEmpty(Convert.ToString(ViewState["TIME"])) ? null : Convert.ToString(ViewState["TIME"]);
        //bean.DishType = string.IsNullOrEmpty(Convert.ToString(ViewState["TYPE"])) ? null : Convert.ToString(ViewState["TYPE"]);
        //bean.DishClass = string.IsNullOrEmpty(Convert.ToString(ViewState["CLASS"])) ? null : Convert.ToString(ViewState["CLASS"]);

        //IList list = dao.FindDish(bean);
        //this.GridView1.DataSource = list;
        //this.GridView1.DataBind();

    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        //从pop画面添加菜肴
        if (!string.IsNullOrEmpty(this.hidFoodsId.Value))
        {
            //取得物资申领单内容
            IList list = null;
            string[] foodsId = this.hidFoodsId.Value.Split(',');
            if (ViewState["DISH_LIST"] == null)
            {
                list = new ArrayList();
            }
            else
            {
                list = (IList)ViewState["DISH_LIST"];
            }


            for (int j = 0; j < foodsId.Length/2;j++ )
            {
                int dishId = Convert.ToInt32(foodsId[j*2]);


                //取得前一画面的申领单数据

                MemberDinnerDetailBean paramBean = null;

                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    paramBean = (MemberDinnerDetailBean)list[i];
                    paramBean.DishQuantity = Convert.ToInt32((this.GridView2.Rows[i].FindControl("quantity") as TextBox).Text);
                }

                //查找单个菜肴信息
                BookFoodDao dao = new BookFoodDao();
                MemberDinnerDetailBean bean = new MemberDinnerDetailBean();
                bean.DishId = dishId;
                bean = dao.GetMemberDinnerDetail(bean);

                //判断是否加入新的物资

                bool flg = true; //true:新物资；false:旧物资
                for (int i = 0; i < list.Count; i++)
                {
                    paramBean = (MemberDinnerDetailBean)list[i];
                    if (bean.DishId == paramBean.DishId)
                    {
                        paramBean.DishQuantity = paramBean.DishQuantity == 99999 ? paramBean.DishQuantity : paramBean.DishQuantity + 1;
                        flg = false;
                        break;
                    }
                }

                if (flg)
                {
                    bean.DishQuantity = 1;
                    list.Add(bean);
                }




                ViewState["DISH_LIST"] = list;
                this.GridView2.DataSource = list;
                this.GridView2.DataBind();

                this.hidFoodsId.Value = "";

            }

            
        }

        //tan add
        if (GridView2.Rows.Count > 0)
        {
            decimal dSum = 0;
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                decimal dPrice = Convert.ToDecimal(GridView2.Rows[i].Cells[4].Text);
                TextBox quantity = ((TextBox)GridView2.Rows[i].FindControl("quantity"));
                int iQuantity = Convert.ToInt32(quantity.Text);
                dSum = dSum + (dPrice * iQuantity);
            }
            lblSum.Text = "小计：" + dSum + "元";
        }

        if (GridView2.Rows.Count > 0)
        {
            hidPrice.Value = "";
            hidCid.Value = "";
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                if (hidPrice.Value == "")
                {
                    hidPrice.Value = Convert.ToString(GridView2.Rows[i].Cells[4].Text);
                }
                else
                {
                    hidPrice.Value = hidPrice.Value + "," + Convert.ToString(GridView2.Rows[i].Cells[4].Text);
                }

                TextBox quantity = ((TextBox)GridView2.Rows[i].FindControl("quantity"));

                if (hidCid.Value == "")
                {
                    hidCid.Value = quantity.ClientID;
                }
                else
                {
                    hidCid.Value = hidCid.Value + "," + quantity.ClientID;
                }
            }
        }

    }
}
